package cn.ihelloworld.controller;

import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ihelloworld.bean.Comment;
import cn.ihelloworld.dao.Pagation;
import cn.ihelloworld.service.ICommentService;
import cn.ihelloworld.util.JSONResult;

@Controller
@RequestMapping("/admin/comment/")
public class CommentController {
	@Autowired
	private ICommentService commentService;
	
	@RequestMapping("index")
	public String index(){
		return "admin/comment";
	}
	
	@RequestMapping("query")
	@ResponseBody
	public JSONResult<Comment> query(Comment comment,Integer page,Integer rows){
		JSONResult<Comment> json=new JSONResult<Comment>();
		Pagation<Comment> paga=commentService.selectForPage(comment, page, rows);
		json.setRows(paga.getRs());
		json.setTotal(paga.getTotal());
		return json;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public JSONResult<Comment> modify(String list){
		JSONResult<Comment> json=new JSONResult<Comment>();
		System.out.println(list);
		JSONArray jsonarray=JSONArray.fromObject(list);
		List<Comment> comments=JSONArray.toList(jsonarray,Comment.class);
		commentService.saveOrUpdate(comments);
		json.setStatus(true);
		return json;
	}
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult<Comment> delete(String ids){
		JSONResult<Comment> json=new JSONResult<Comment>();
		String[] array=ids.split(",");
		commentService.delete(array);
		json.setStatus(true);
		return json;
	}
}
